//
//  ClockHeaderView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/12.
//

#import "ClockHeaderView.h"
#import "TemplateFoundDetailsModel.h"

@interface ClockHeaderView ()

@property (nonatomic ,strong) UIView * backview;
@property (nonatomic ,strong) UILabel * nameLab;
@property (nonatomic ,strong) UILabel * executeTime;
@property (nonatomic ,strong) UILabel * tissueName;
@property (nonatomic ,strong) UILabel * Description;
@property (nonatomic ,strong) UILabel * appointorName;
@property (nonatomic ,strong) UIView * btnview;

@end
@implementation ClockHeaderView


+ (instancetype)NewView {
    ClockHeaderView * view = [ClockHeaderView new];
    view.clipsToBounds = YES;
    [view newUI];
    return view;
}
- (void)newUI{
    
    MJWeakSelf
    UIView * backview = [UIView new];
    self.backview = backview;
    [self addSubview:backview];
    backview.backgroundColor = UIColor.whiteColor;
    [DTKTool YJ:kwt(5) andView:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(kwt(8));
        make.right.offset(kwt(-8));
        make.height.offset(kwt(100));
    }];
    
    UILabel * nameLab = [DTKTool labelWithText:@"-" andFont:kCFont(19) andTextColor:KcolorHex(@"#222222")];
    self.nameLab = nameLab;
    [backview addSubview:nameLab];
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.offset(kwt(14));
    }];
    
    UIImageView * leftimg = kimageView(@"time");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(13));
        make.top.offset(kwt(48));
    }];
    
    
    UILabel * executeTime = [DTKTool labelWithText:@"-" andFont:kCFont(17) andTextColor:KcolorHex(@"#0967FF")];
    self.executeTime = executeTime;
    [backview addSubview:executeTime];
    [executeTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(36));
        make.right.offset(kwt(-10));
        make.centerY.equalTo(leftimg);
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#DBDBDB");
    [backview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.right.offset(kwt(-12));
        make.top.offset(kwt(74));
        make.height.offset(0.5);
    }];
    
    UILabel * tissueName = [DTKTool labelWithText:@"-" andFont:kCFont(16) andTextColor:KcolorHex(@"#222222")];
    self.tissueName = tissueName;
    tissueName.numberOfLines = 0;
    [backview addSubview:tissueName];
    [tissueName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.right.offset(kwt(-12));
        make.top.offset(kwt(85));
    }];
    
    UILabel * Description = [DTKTool labelWithText:@"-" andFont:kFont(16) andTextColor:KcolorHex(@"#787A79")];
    self.Description = Description;
    Description.numberOfLines = 0;
    [backview addSubview:Description];
    [Description mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.width.offset(kwt(335));
        make.top.equalTo(tissueName.mas_bottom).offset(kwt(9));
    }];
    
    UILabel * appointorName = [DTKTool labelWithText:@"-" andFont:kCFont(16) andTextColor:KcolorHex(@"#222222")];
    self.appointorName = appointorName;
    appointorName.numberOfLines = 0;
    [backview addSubview:appointorName];
    [appointorName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.width.offset(kwt(335));
        make.top.equalTo(Description.mas_bottom).offset(kwt(10));
    }];
    
    UIView * buttomview = [UIView new];
    buttomview.backgroundColor = UIColor.whiteColor;
    [self addSubview:buttomview];
    [DTKTool BFYJ:kwt(10) andView:buttomview andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize:CGRectMake(0,0,SCREEN_WIDTH, kwt(50))];
    [buttomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(kwt(50));
        make.top.equalTo(backview.mas_bottom).offset(kwt(8));
    }];
    
    UILabel * templab = [DTKTool labelWithText:@"打卡内容" andFont:kFont(19) andTextColor:KcolorHex(@"#222222")];
    [buttomview addSubview:templab];
    [templab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(15));
        make.height.offset(kwt(20));
        make.left.offset(kwt(12));
    }];
    
    UIButton * ycbtn = [UIButton buttonWithType:0];
    ycbtn.addNormalText(@"上报异常").addNormalColor(UIColor.whiteColor).addFont(kCFont(16)).addbackgroundColor(KcolorHex(@"#FF8800")).addTarget(self,@selector(openYC));
    [DTKTool YJ:kwt(5) andView:ycbtn];
    [buttomview addSubview:ycbtn];
    [ycbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(templab);
        make.right.offset(kwt(-12));
        make.width.offset(kwt(93));
        make.height.offset(kwt(34));
    }];
    
    UIView * btnview = [UIView new];
    self.btnview = btnview;
    btnview.backgroundColor = UIColor.whiteColor;
    [self addSubview:btnview];
    [btnview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.top.equalTo(buttomview.mas_bottom);
        make.height.offset(kwt(200));
    }];
    
}
- (void)setModel:(ClockModel *)model{
    _model = model;
    self.nameLab.text = model.name;
    self.executeTime.text = model.executeTime;
    self.tissueName.text = model.tissueNameStr;//[NSString stringWithFormat:@"%@ %@",model.tissueName,model.pondModelList.firstObject.name];
    [self.tissueName mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(model.tissueNameStrH);
    }];
    self.Description.text = [NSString stringWithFormat:@"任务说明：%@",model.Description];
    [self.Description mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(model.DescriptionStrH);
    }];
    self.appointorName.text = model.appointorNameStr;

    [self.backview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(model.HeaderTopView);
    }];
    [self.btnview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(model.superTaskModel.TYDKDXXZViewH);
    }];
    if(model.configureItem == 1){
        [self.btnview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UIView * dxview = [UIView new];
        dxview.backgroundColor = KcolorHex(@"#F6F7F9");
        [DTKTool YJ:kwt(5) andView:dxview];
        [self.btnview addSubview:dxview];
        [dxview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
            make.left.offset(kwt(10));
            make.right.offset(kwt(-10));
            make.bottom.offset(kwt(-10));
        }];
      
        UILabel * lab = [DTKTool labelWithText:model.superTaskModel.name andFont:kFont(17) andTextColor:KcolorHex(@"#222222")];
        lab.attributedText = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"%@ (可多选)",model.superTaskModel.name] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#7F7F7F") andFont:kCFont(18) andPattenr:@"(\\(可多选\\))"];
        [dxview addSubview:lab];
        lab.textAlignment = NSTextAlignmentCenter;
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(kwt(12));
            make.top.offset(kwt(14));
        }];
        
        if(model.superTaskModel.forceFlag== 1){
            UILabel * templab = [DTKTool labelWithText:@"*" andFont:kCFont(18) andTextColor:KcolorHex(@"#FA0F0F")];
            [dxview addSubview:templab];
            [templab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(kwt(12));
                make.top.offset(kwt(14));
            }];
            [lab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(templab.mas_right);
            }];
            
        }
        CGFloat dxY = kwt(45);
        CGFloat X = kwt(10);
        MJWeakSelf
        for (TemplateFoundDetailsItemModelListModel * childModel in model.superTaskModel.itemModelList) {
            UIView * btnview = [UIView new];
            btnview.addClickBlock(^{
                [weakSelf btnclick:childModel];
            });
            [DTKTool YJBK:kwt(6) andView:btnview andWidth:1 andColor:childModel.checkedFlag?KcolorHex(@"#0967FF"):KcolorHex(@"#BFBFBF")];
            UILabel * lab = [DTKTool labelWithText:childModel.name andFont:kFont(16) andTextColor:childModel.checkedFlag?KcolorHex(@"#0967FF"):KcolorHex(@"#373C45")];
            [btnview addSubview:lab];
            lab.textAlignment = NSTextAlignmentCenter;
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(kwt(10));
                make.right.offset(kwt(-10));
                make.centerY.offset(0);
            }];
            if(childModel.checkedFlag){
                UIImageView * xzimg = kimageView(@"template_selete");
                [btnview addSubview:xzimg];
                [xzimg mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.right.bottom.offset(0);
                    make.height.offset(kwt(11));
                    make.width.offset(kwt(10));
                }];
            }
            
            [dxview addSubview:btnview];
            if(X + childModel.nameW > kwt(340)){
                X = kwt(10);
                dxY += kwt(48);
            }
            btnview.frame = CGRectMake(X, dxY, childModel.nameW, kwt(32));
            X += childModel.nameW + kwt(14);
        }
        
    }
}

- (void)btnclick:(TemplateFoundDetailsItemModelListModel *)model{
    //    if(self.model.pattern == 1){//是单选
    //        for (TemplateFoundDetailsItemModelListModel * childModel in self.model.itemModelList) {//子选项
    //            if(childModel == model){
    //                model.checkedFlag = (model.checkedFlag == 1)?0:1;
    //            } else {
    //                childModel.checkedFlag = 0;
    //            }
    //        }
    //    } else {
    model.checkedFlag = (model.checkedFlag == 1)?0:1;
    //    }
    
    if(self.openBtnClickBlock){
        self.openBtnClickBlock(self.model.superTaskModel);
    }
}
- (void)openYC{
    if(self.openYCBlock){
        self.openYCBlock();
    }
}
@end
